package com.whfc.wxmp.api.emp.service.impl;

import com.whfc.common.result.PageData;
import com.whfc.emp.dto.AppWarnEmpRecordDTO;
import com.whfc.emp.dto.AppWarnEmpRecordDetailDTO;
import com.whfc.emp.dto.AppWarnEmpRecordNumDTO;
import com.whfc.emp.dto.train.AppWxWarnEmpRecordNumDTO;
import com.whfc.emp.dto.train.AppWxWarnEmpRecordNumDTOV2;
import com.whfc.emp.enums.AppEmpWarnRuleType;
import com.whfc.emp.param.AppWarnHandleParam;
import com.whfc.emp.service.AppEmpWarnService;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.wxmp.api.emp.service.WxEmpWarnService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author sun_guodong
 * @description
 * @date 2020-06-30
 */
@Service
public class WxEmpWarnServiceImpl implements WxEmpWarnService {

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = AppEmpWarnService.class, version = "1.0.0")
    private AppEmpWarnService appEmpWarnService;

    @Override
    public PageData<AppWarnEmpRecordDTO> empWarnList(Integer deptId, Integer pageNum, Integer pageSize, Integer handleState,Integer ruleType,Date startDate,Date endDate) {
        return appEmpWarnService.recordList(deptId, pageNum, pageSize, handleState,ruleType,startDate,endDate);
    }

    @Override
    public AppWarnEmpRecordDetailDTO getEmpWarnDetail(Integer warnId) {
        AppWarnEmpRecordDetailDTO detailDTO = appEmpWarnService.recordDetail(warnId);
        return detailDTO;
    }

    @Override
    public void handleEmpWarn(WxUser wxUser, AppWarnHandleParam body) {
        appEmpWarnService.handle(wxUser.getId(), wxUser.getNickName(), wxUser.getPhone(), body);
    }


    @Override
    public AppWxWarnEmpRecordNumDTOV2 num(Integer deptId, Date startTime, Date endTime) {
        AppWxWarnEmpRecordNumDTOV2 retDTO = new AppWxWarnEmpRecordNumDTOV2();

        AppWarnEmpRecordNumDTO appWarnEmpRecordNumDTO = appEmpWarnService.empWarnNum(deptId, 0, startTime, endTime);
        AppWarnEmpRecordNumDTO appWarnEmpRecordNumDTO1 = appEmpWarnService.empWarnNum(deptId, 1, startTime, endTime);
        List<AppWarnEmpRecordNumDTO> warnEmpNumUnHandle = appWarnEmpRecordNumDTO.getWarnEmpNum();
        List<AppWarnEmpRecordNumDTO> warnEmpNumHandle = appWarnEmpRecordNumDTO1.getWarnEmpNum();
        Map<Integer,Integer> handleMap = new HashMap<>();
        Map<Integer,Integer> unHandleMap = new HashMap<>();

        for (AppWarnEmpRecordNumDTO a:warnEmpNumUnHandle) {
            Integer ruleType = a.getRuleType();
            Integer num = a.getNum();
            unHandleMap.put(ruleType,num);
        }

        for (AppWarnEmpRecordNumDTO a:warnEmpNumHandle) {
            Integer ruleType = a.getRuleType();
            Integer num = a.getNum();
            handleMap.put(ruleType,num);
        }

        List<AppWarnEmpRecordNumDTO> retHandleList = new ArrayList<>();
        List<AppWarnEmpRecordNumDTO> retUnHandleList = new ArrayList<>();
        AppEmpWarnRuleType[] values = AppEmpWarnRuleType.values();
        for (AppEmpWarnRuleType appEmpWarnRuleType:values) {
            Integer value = appEmpWarnRuleType.value();
            AppWarnEmpRecordNumDTO warnEmpNumUnHandleDTO = new AppWarnEmpRecordNumDTO();
            AppWarnEmpRecordNumDTO warnEmpNumHandleDTO = new AppWarnEmpRecordNumDTO();
            warnEmpNumUnHandleDTO.setRuleType(value);
            warnEmpNumUnHandleDTO.setRuleName(appEmpWarnRuleType.tittle());
            warnEmpNumHandleDTO.setRuleType(value);
            warnEmpNumHandleDTO.setRuleName(appEmpWarnRuleType.tittle());
            warnEmpNumUnHandleDTO.setNum(unHandleMap.getOrDefault(value,0));
            warnEmpNumHandleDTO.setNum(handleMap.getOrDefault(value,0));
            retHandleList.add(warnEmpNumHandleDTO);
            retUnHandleList.add(warnEmpNumUnHandleDTO);
        }



        retDTO.setHandleNum(appWarnEmpRecordNumDTO1.getTotal());
        retDTO.setUnHandleNum(appWarnEmpRecordNumDTO.getTotal());
        retDTO.setHandleWarnList(retHandleList);
        retDTO.setUnHandleWarnList(retUnHandleList);
        return retDTO;


    }
}
